package com.tan.connection.datatype;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import redis.clients.jedis.Jedis;

public class RedisList {
    private static Logger logger =LogManager.getLogger(RedisList.class.getName());
    public static void main(String[] args) {
        Jedis jedis = new Jedis("172.21.50.136",6379);
        logger.info("清空数据库：");
        jedis.flushDB();

        logger.info("================添加一个list===================");
        jedis.lpush("collections","ArrayList","Vector","Stack","HashMap","WeakHashMap");
        jedis.lpush("collections","HashSet","TreeSet","TreeMap");
        logger.info("collection的内容："+jedis.lrange("collections",0,-1));
        logger.info("collections区间0-3的元素："+jedis.lrange("collections",0,3));
        logger.info("===================================================");

        //删除列表指定的值，第二个参数为删除的个数（有重复时），后add进去的值先被删除，类似于出栈
        logger.info("删除指定元素个数："+jedis.lrem("collections",2,"HashMap"));
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("删除下表0-3区间之外的元素："+jedis.ltrim("collections",0,3));//截取出来。截取后原Key不在存储
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("collections列表出栈（左端）："+jedis.lpop("collections"));
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("collections添加元素，从列表右端，与lpush相对应："+jedis.rpush("collections","newValue"));
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("collections列表出栈，右端："+jedis.rpop("collections"));
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("修改指定下标1的内容："+jedis.lset("collections",1,"setChangeValue"));
        logger.info("collections的内容："+jedis.lrange("collections",0,-1));
        logger.info("-----------------------------------------");
        logger.info("collections的长度："+jedis.llen("collections"));
        logger.info("获取collections下标为2的元素："+jedis.lindex("collections",2));
        logger.info("-----------------------------------------");
        logger.info("排序：");
        jedis.lpush("sortedList","3","2","4","1","8","6");
        logger.info("sortedList排序前："+jedis.lrange("sortedList",0,-1));
        logger.info(jedis.sort("sortedList"));



    }
}
